package com.example.xml;

import android.os.Handler;
import com.example.modle.Mp3Info;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.sql.SQLOutput;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Jasic
 * Date: 12-11-8
 */
public class Mp3ListXmlHandler extends DefaultHandler {

    private Mp3Info mp3Info = null;
    private List<Mp3Info> mp3infos = null;
    private String tagName = null;

    public Mp3ListXmlHandler(List<Mp3Info> mp3infos) {
        this.mp3infos = mp3infos;
    }

    /**
     * 开始整个文档
     *
     * @throws SAXException
     */
    @Override
    public void startDocument() throws SAXException {

//        System.out.println("第一步：开始解析文档");
        super.startDocument();    //To change body of overridden methods use File | Settings | File Templates.
    }

    /**
     * @param uri
     * @param localName
     * @param qName
     * @param attributes
     *
     * @throws SAXException
     */
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        this.tagName = localName;

//        System.out.println("第二步：开始解析元素");
//        System.out.println("uri :" + uri + "," +
//                "localName :" + localName + "," +
//                "qName :" + qName + "," +
//                "attributes :" + attributes + ","
//        );

        if (tagName.equals("resource")) {

            this.mp3Info = new Mp3Info();
        }


    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {

//        System.out.println("第三步：详细解析元素内容...");
//        System.out.println("start " + start);
//        System.out.println("length " + length);
//        System.out.println("[字符长度:]" + ch.length + "], 字符串内容[" + new String(ch, start, length) + "]");

        String eleName = new String(ch, start, length);

//        System.out.println("char length:" + ch.length);
//        System.out.println("element name :" + eleName);

        if ("id".equals(this.tagName)) {
            mp3Info.setId(eleName);
        } else if ("mp3.name".equals(this.tagName)) {

            mp3Info.setMp3_Name(eleName);
        } else if ("mp3.size".equals(this.tagName)) {

            mp3Info.setMp3_Size(eleName);
        } else if ("lrc.name".equals(this.tagName)) {

            mp3Info.setLrc_Name(eleName);
        } else if ("lrc.size".equals(this.tagName)) {

            mp3Info.setLrc_Size(eleName);
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {

//        System.out.println("第四步：结束解析元素....");
        if (qName.equals("resource")) {

//            System.out.println("得到一个元素为：" + mp3Info);
            mp3infos.add(mp3Info);
        }

        this.tagName = "";
    }

    /**
     * 结束文档
     *
     * @throws SAXException
     */
    @Override
    public void endDocument() throws SAXException {

//        System.out.println("第五步步：结束解析文档");
        super.endDocument();    //To change body of overridden methods use File | Settings | File Templates.
    }

}
